package com.yfp.client.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.client.domain.MarCoupon;
import com.yfp.client.domain.StoredValueActivity;
import com.yfp.client.domain.StoredValueActivityDetail;
import com.yfp.client.domain.vo.StoredValueActivityDetailVO;
import com.yfp.client.domain.vo.StoredValueActivityVO;
import com.yfp.client.enums.StoredValueActivityTypeEnum;
import com.yfp.client.mapper.StoredValueActivityMapper;
import com.yfp.client.service.MarCouponService;
import com.yfp.client.service.StoredValueActivityDetailService;
import com.yfp.client.service.StoredValueActivityService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 *
 * @description 针对表【stored_value_activity(储值活动)】的数据库操作Service实现
 * @createDate 2024-11-22 10:15:05
 */
@Service
public class StoredValueActivityServiceImpl extends ServiceImpl<StoredValueActivityMapper, StoredValueActivity> implements StoredValueActivityService {

    @Resource
    private StoredValueActivityMapper storedValueActivityMapper;
    @Resource
    private StoredValueActivityDetailService storedValueActivityDetailService;
    @Resource
    private MarCouponService marCouponService;


    @Override
    public StoredValueActivityVO getOngoingActivity() {
        // 获取正在进行中的储值活动
        StoredValueActivity storedValueActivity = storedValueActivityMapper.getOngoingActivity();
        if (Objects.isNull(storedValueActivity)) {
            return null;
        }
        List<StoredValueActivityDetail> storedValueActivityDetails = storedValueActivityDetailService.listByActivityId(storedValueActivity.getId());
        List<StoredValueActivityDetailVO> storedValueActivityDetailVOS = BeanUtil.copyToList(storedValueActivityDetails, StoredValueActivityDetailVO.class);
        List<Long> marCouponIds = storedValueActivityDetailVOS.stream().map(StoredValueActivityDetailVO::getMarCouponId).collect(Collectors.toList());
        List<MarCoupon> marCoupons = marCouponService.findByIds(marCouponIds);

        // 组装优惠券名称
        for (StoredValueActivityDetailVO storedValueActivityDetailVO : storedValueActivityDetailVOS) {
            for (MarCoupon marCoupon : marCoupons) {
                if (Objects.equals(storedValueActivityDetailVO.getMarCouponId(), marCoupon.getId())) {
                    storedValueActivityDetailVO.setMarCouponName(marCoupon.getCouponName());
                }
            }
        }

        StoredValueActivityDetailVO firstStoredActivity = storedValueActivityDetailVOS.stream().filter(item -> Objects.equals(item.getType(), StoredValueActivityTypeEnum.FIRST_STORED_VALUE.getCode())).findFirst().orElse(null);
        List<StoredValueActivityDetailVO> storedActivity = storedValueActivityDetailVOS.stream().filter(item -> Objects.equals(item.getType(), StoredValueActivityTypeEnum.STORED_VALUE.getCode())).sorted(Comparator.comparing(StoredValueActivityDetailVO::getId)).collect(Collectors.toList());

        StoredValueActivityVO storedValueActivityVO = BeanUtil.toBean(storedValueActivity, StoredValueActivityVO.class);
        storedValueActivityVO.setFirstStoredActivity(firstStoredActivity);
        storedValueActivityVO.setStoredActivity(storedActivity);

        return storedValueActivityVO;
    }
}




